<?php

namespace App\Supports\Payment\Methods\Adawxmini;

use Carbon\Carbon;
use App\Models\VipPurchase;
use App\Events\VipPurchasePaidEvent;
use App\Supports\Payment\Methods\Method;
use App\Supports\Payment\Contracts\MethodCallback;

class CallbackVipPay extends Method implements MethodCallback
{
	public function markAsPaid($order = null, $data = null)
	{
		$order->update([
			'paid_at' => Carbon::now(),
			'paid_method' => 'adawxmini',
			'paid_number' => $data['id']
		]);
		event(new VipPurchasePaidEvent($order));
		return response(null, 200);
	}
	
	public function callback($data = null)
	{
		$number = $this->getPaidNumber($data['order_no']);
		
		if (!$purchase = VipPurchase::where('number', $number)->first()) {
			return 'fail';
		}
		
		if ($purchase->isPaid()) {
			return response(null, 200);
		}
		
		return $this->markAsPaid($purchase, $data);
	}
}
